// pages/mclassMusic/mclassMusic.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    state: 'paused',
    playIndex: 0,
    play: {
      currentTime: '00:00',
      duration: '00:00',
      percent: 0,
      title: '',
      singer: '',
      coverImagUrl: '../../images/停止播放.png'
    },
    
  playlist: [{
    id: 1,title:'神兵小将',singer:'不怕',
    src: '../../musics/千里狗 - 不怕（《神兵小将》片尾曲）（翻自 赵蓓）.mp3', coverImagUrl:'../../images/耳机.png'
  },{
    id: 2,title:'春三月',singer:'司南',
    src: '../../musics/司南 - 春三月.mp3', coverImagUrl:'../../images/耳机.png'
  },{
    id: 3,title:'神兵小将2',singer:'不怕2',
    src: '../../musics/千里狗 - 不怕（《神兵小将》片尾曲）（翻自 赵蓓）.mp3', coverImagUrl:'../../images/耳机.png'
  },{
    id: 4,title:'春三月2',singer:'司南2',
    src: '../../musics/司南 - 春三月.mp3', coverImagUrl:'../../images/耳机.png'
  }
],
  },
  // 轮播+按钮---------------------------------------------------------
  onLoad: function(options){
    wx.request({
      url: 'http://apis.juhe.cn/simpleWeather/query?',
      data: {
        "city":"成都",
        "key":"3a6eb55aeb408bb0aea44337e79bfaaa"
      },
      success(res){
        console.log(res);
        
      }
    })
  },
  // 頂部----------------------------------------------------------------
  changeItem: function(e){
    console.log("changeItem");
    this.setData({item: e.target.dataset.item});
  },
  change: function(e){
    console.log("change");
    this.setMusic(e.currentTarget.dataset.index);
    this.play();    
  },
  changePage: function(e){
    this.setData({item: 2});
  },
  changeTab: function(e){
    console.log("changeTab");
    this.setData({tab: e.detail.current});
  },
   //底部播放器---------------------------------------------------------------------
// 暂停播放事件处理
play:function(){
  this.audioCtx.play()
  this.setData({state:'running'})
},
pause:function(){
  this.audioCtx.pause()
  this.setData({state:'paused'})
},

// 下一曲事件处理
next:function(){
  var index=this.data.playIndex >= this.data.playlist.length -1 ? 0 : this.data.playIndex + 1
  this.setMusic(index)
  if(this.data.state === 'running'){
    this.play()
  }
},
// 控制播放进度---------------------------------------------------------------
// 控制进度条和时间显示
onReady:function(){
  this.audioCtx=wx.createInnerAudioContext()
  var that=this
  // 播放失败检测
  this.audioCtx.onError(function(){
    console.log('播放失败：'+ that.audioCtx.src)
  })
  // 播放完自动切换到下一首
  this.audioCtx.onEnded(function(){
    that.next()
  })
  // 自动更新播放进度
  this.audioCtx.onPlay(function(){
    that.audioCtx.onTimeUpdate(function(){
      that.setData({
        'play.duration':formatTime(that.audioCtx.duration),
        'play.currentTime':formatTime(that.audioCtx.currentTime),
        'play.percent':that.audioCtx.currentTime / that.audioCtx.duration * 100
      })
    })
  })
  // 默认选择第一曲
  this.setMusic(0)
  // 格式化时间
  function formatTime(time){
    var minute = Math.floor(time / 60) % 60;
    var second = Math.floor(time) % 60;
    return (minute < 10 ? '0' + minute : minute) + ':' + (second <10 ? '0' + second : second)
  }
},
sliderChange:function(e){
  var second=e.detail.value * this.audioCtx.duration / 100
  this.audioCtx.seek(second)
},
//实现换曲功能---------------------------------------------
change:function(e){
  this.setMusic(e.currentTarget.dataset.index)
  this.play()
},
//实现音乐播放功能 -----------------------------------------------------------
    //自动播放第一条曲目
  audioCtx: null,
  onReady: function () {
    this.audioCtx = wx.createInnerAudioContext()
    //默认选择第一首
    this.setMusic(0)
  },
  setMusic: function(index){
    var music = this.data.playlist[index]
    this.audioCtx.src = music.src
    this.setData({
      playIndex: index,
      'play.title': music.title,
      'play.singer': music.singer,
      'play.coverImgUrl': music.coverImagUrl,
      'play.currentTime': '00:00',
      'play.duration': '00:00',
      'play.percent': 0 
    })
  },
})